<?php
class Ucenter_con extends Lxbase{
	private $uconn;
	private $cid;
	
	public function __construct($cid=1){
		$ucarr = array("host"=>'103.7.222.62',"port"=>3306,"user"=>'root',"pass"=>'zhs20151117',"name"=>'ucenter',"pdo"=>true);
		$this->uconn = new Lxbase($ucarr);
		$this->cid=$cid;
		parent::__construct();
	}
	
	public function user(){
		//C端用户列表
		$list = $this->uconn->arrdata("*","user","where userid={$this->userinfo['userid']} and cid={$this->cid}");
		$this->smarty->assign("navstyle",array("user"=>"class='sel'"));
		$this->smarty->assign("list",$list);
		$this->smarty->assign("tit",'用户列表');
		$this->smarty->display("user.tpl");
	}
	
	
	public function settool(){
		//功能设置
		if(!empty($_POST)){
			$uset = $_POST["uset"];
			$id=$_POST["id"];
			$url=$_POST["url"];
			$title=$_POST["title"];
			$icon=$_POST["icon"];
			if(!empty($uset)){
				$sid=implode(',',$uset);
			}else{
				$sid='';
			}
			foreach($title as $k=>$v){
				if(empty($icon[$k])){die(jsfshow('请填写'.$v.'的图标'));}
				if(!empty($id[$k])){
					$into="title='".$title[$k]."',url='".$url[$k]."',icon='".$icon[$k]."'";
					$this->uconn->updata("showset",$into,"where id={$id}");
					$sid.=','.$id[$k];
				}else{
					$into="userid={$this->userinfo['userid']},title='".$title[$k]."',cid={$this->cid},url='".$url[$k]."',icon='".$icon[$k]."',type=2,status=1";
					$newid = $this->uconn->setdata("showset",$into);
					$sid.=','.$newid;
				}
			}
			$ishave = $this->uconn->viewdata("id","userset","where cid={$this->cid} and userid={$this->userinfo['userid']}");
			$sid=trim($sid,',');
			if(empty($ishave)){
				$this->uconn->setdata("userset","cid={$this->cid},userid={$this->userinfo['userid']},sid='{$sid}'");
			}else{
				$this->uconn->updata("userset","sid='{$sid}'","where id={$ishave['id']}");
			}
			die("<script>alert('更新成功');parent.location.href='?g=ucenter&m=ucenter&a=settool&0.".rand(1000,9999)."'</script>");
		}
		$list = $this->uconn->arrdata("*","showset","where cid={$this->cid} and type=1");
		$ulist = $this->uconn->arrdata("*","showset","where cid={$this->cid} and type=2 and userid={$this->userinfo['userid']}");
		$setlist = $this->uconn->viewdata("*","userset","where cid={$this->cid} and userid={$this->userinfo['userid']}");
		if(!empty($setlist)){
			$setarr = explode(",",$setlist["sid"]);
			if(!empty($setarr)){
				$this->smarty->assign("setarr",$setarr);
			}
		}
		$this->smarty->assign("list",$list);
		$this->smarty->assign("ulist",$ulist);
		$this->smarty->assign("maxnum",count($ulist));
		$this->smarty->assign("setlist",$setlist);
		$this->smarty->assign("navstyle",array("tool"=>"class='sel'"));
		$this->smarty->assign("tit","功能设置");
		$this->smarty->display("tool.tpl");
	}
	
	//AJAX上传那图片
	public function uploadpic(){
		if(empty($_FILES["img"]["size"])){
			die(json_encode(array("error"=>1001,"errmsg"=>"请上传正确格式的图片")));
		}
		if($_FILES["img"]["size"] > 5000000){
			die(json_encode(array("error"=>1002,"errmsg"=>"请上传小于5M的图片")));
		}
		
		$picdata = $this->upload($_FILES["img"]);
		if(empty($picdata["pic"])){
			die(json_encode(array("error"=>1003,"errmsg"=>"图片上传失败")));
		}
		$data["error"]=1000;
		$data["pic"] = $picdata["pic"];
		echo json_encode($data);
		exit;
	}
	
	//改变加载功能
	public function cstatus(){
		$id=intval($_GET["id"]);
		$status=intval($_GET["status"]);
		$view = $this->uconn->viewdata("*","userset","where cid={$this->cid} and userid={$this->userinfo['userid']}");
		if(!empty($view["sid"])){
			$sarr=explode(',',$view["sid"]);
			if($status==1){
				$sarr[]=$id;
			}else{
				$key=array_search($id,$sarr);
				unset($sarr[$key]);
			}
			if(!empty($sarr)){
				$sid=implode(',',$sarr);
			}else{
				$sid='';
			}
		}else{
			if($status==1){
				$sid=$id;
			}else{
				$sid='';
			}
		}
		$this->uconn->updata("userset","sid='{$sid}'","where cid={$this->cid} and userid={$this->userinfo['userid']}");
		die("<script>parent.location.href='?g=ucenter&m=ucenter&a=settool&0.".rand(1000,9999)."'</script>");
	}
	
	//删除自定义功能
	public function deltool($id){
		$id=intval($_GET["id"]);
	}
	
}